package com.adminsystem.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.adminsystem.common.aop.AutoLog;
import com.adminsystem.common.utils.JWTUtil;
import com.adminsystem.entity.SysUser;
import io.jsonwebtoken.Claims;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.adminsystem.entity.SysDictType;
import com.adminsystem.service.ISysDictTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 字典类型Controller
 *
 * @author admin
 * @date 2023-11-16
 */
@RestController
@RequestMapping("/sys/dictType")
public class SysDictTypeController extends BaseController
{
    @Autowired
    private ISysDictTypeService sysDictTypeService;

    /**
     * 查询字典类型列表
     */
    @PreAuthorize("hasAuthority('sys:dictType:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysDictType sysDictType)
    {
        startPage();
        List<SysDictType> list = sysDictTypeService.selectSysDictTypeList(sysDictType);
        return getDataTable(list);
    }

    /**
     * 导出字典类型列表
     */
    @PreAuthorize("hasAuthority('sys:dictType:export')")
    @Log(title = "字典类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDictType sysDictType)
    {
        List<SysDictType> list = sysDictTypeService.selectSysDictTypeList(sysDictType);
        ExcelUtil<SysDictType> util = new ExcelUtil<SysDictType>(SysDictType.class);
        util.exportExcel(response, list, "字典类型数据");
    }

    /**
     * 获取字典类型详细信息
     */
    @PreAuthorize("hasAuthority('sys:dictType:query')")
    @GetMapping(value = "/{dictId}")
    public AjaxResult getInfo(@PathVariable("dictId") Long dictId)
    {
        return success(sysDictTypeService.selectSysDictTypeByDictId(dictId));
    }

    /**
     * 新增字典类型
     */
    @PreAuthorize("hasAuthority('sys:dictType:add')")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody SysDictType sysDictType)
    {
        String token=request.getHeader("token");
        Claims claims = JWTUtil.parseJWT(token);
        sysDictType.setCreateBy(String.valueOf(claims.getId()));
        return toAjax(sysDictTypeService.insertSysDictType(sysDictType));
    }

    /**
     * 修改字典类型
     */
    @PreAuthorize("hasAuthority('sys:dictType:edit')")
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(HttpServletRequest request,@RequestBody SysDictType sysDictType)
    {
        String token=request.getHeader("token");
        Claims claims = JWTUtil.parseJWT(token);
        sysDictType.setUpdateBy(String.valueOf(claims.getId()));
        return toAjax(sysDictTypeService.updateSysDictType(sysDictType));
    }

    /**
     * 删除字典类型
     */
    @PreAuthorize("hasAuthority('sys:dictType:remove')")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{dictIds}")
    public AjaxResult remove( @PathVariable Long[] dictIds)
    {
        return toAjax(sysDictTypeService.deleteSysDictTypeByDictIds(dictIds));
    }
    @PostMapping("/updateStatus")
    @PreAuthorize("hasAuthority('sys:dictType:edit')")
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    public AjaxResult updateUserStatus(@RequestBody SysDictType sysDictType) {
        SysDictType currentSysDictType = sysDictTypeService.getById(sysDictType.getDictId());
        currentSysDictType.setUpdateTime(new Date());
        currentSysDictType.setStatus(sysDictType.getStatus());
        return toAjax(sysDictTypeService.updateById(currentSysDictType));
    }
}
